using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using Commerce.Promotions;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.media;

namespace CommerceForUmbraco.usercontrols
{
    public partial class ProductBox : System.Web.UI.UserControl
    {
        protected string ProductName;
        protected decimal RetailPrice;
        protected decimal OurPrice;
        protected string ImageFile = "";
        protected string CategoryName = "";
        protected string productGUID;

        private Commerce.Common.Product product = null;
        private Commerce.Common.Category category = null;

        // umbraco properties
        private int _umbProductId;

        public int umbProductId
        {
            get { return _umbProductId; }
            set { _umbProductId = value; }
        }

        private int _umbCategoryId;

        public int umbCategoryId
        {
            get { return _umbCategoryId; }
            set { _umbCategoryId = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (_umbProductId != 0)
                {
                    product = ProductController.GetProductDeep(_umbProductId);
                }

                // make sure we have a product
                TestCondition.IsTrue(product.IsLoaded, "Invalid url/product id");

                // set the category
                if (_umbCategoryId != 0)
                {
                    CategoryController.Load();
                    category = CategoryController.Find(_umbCategoryId);
                    CategoryName = category.CategoryName;
                }

                // set the page variables
                ProductName = product.ProductName;
                RetailPrice = product.RetailPrice;
                OurPrice = product.OurPrice;
                // get the image URL from the Umbraco Node id
                Media m = new Media(int.Parse(product.DefaultImage));
                ImageFile = m.getProperty("umbracoFile").Value.ToString();
                productGUID = product.ProductGUID.ToString();

            }
            catch (Exception)
            {
                // set up the safe display
                CategoryName = "Coming Soon";
                ProductName = "Coming Soon";
                RetailPrice = 0;
                OurPrice = 0;
            }

        }
    }
}